package org.nightrunner.d3.controller.management;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nightrunner.d3.constant.CRUDConstant;
import org.nightrunner.d3.entity.Category;
import org.nightrunner.d3.entity.Equipment;
import org.nightrunner.d3.service.ICategoryService;
import org.nightrunner.d3.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
 * Search Page Controller
 * 
 * @author haiyang.chen
 * @date 2012-5-15
 * @version 1.0
 */
@Controller
@RequestMapping("management/search/")
public class SearchManagementController implements CRUDConstant {

	private static final String RETURN_VIEW_PREFIX = "management/search/";

	@Autowired
	private ISearchService searchService;

	public ISearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(ISearchService searchService) {
		this.searchService = searchService;
	}

	@Autowired
	private ICategoryService categoryService;

	public ICategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(ICategoryService categoryService) {
		this.categoryService = categoryService;
	}

	@RequestMapping("index")
	public String show(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
		List<String> hotKeywords = new ArrayList<String>();
		for (int i = 0; i < 5; i++) {
			hotKeywords.add("关键字" + i);
		}

		modelMap.addAttribute("hotKeywords", hotKeywords);

		return RETURN_VIEW_PREFIX + VIEW_INDEX;
	}

	// @RequestMapping(value = "view/{keyword}-{currentPage}-{rowSize}", method
	// = RequestMethod.GET)
	// public String result(ModelMap modelMap,
	// @PathVariable("keyword") String keyword,
	// @PathVariable("currentPage") Integer currentPage,
	// @PathVariable("rowSize") Integer rowSize) throws Exception {
	// if (keyword == null || keyword.trim().length() == 0) {
	// return "redirect:/" + RETURN_VIEW_PREFIX + VIEW_INDEX;
	// }
	// List<Equipment> matters = searchService.searchEquipments(keyword);
	// modelMap.addAttribute("keyword", keyword);
	// modelMap
	// .addAttribute("results", ResultViewUtil.getResultViews(matters));
	//
	// return RETURN_VIEW_PREFIX + VIEW_VIEW;
	// }

	@RequestMapping(value = "equipment/presearchforcte", method = RequestMethod.GET)
	public String preSearchForMmcUpdate(ModelMap modelMap) throws Exception {
		return RETURN_VIEW_PREFIX + "equipment/searchForCte";
	}

	@RequestMapping(value = "equipment/presearchfornode", method = RequestMethod.GET)
	public String preSearchForNode(ModelMap modelMap) throws Exception {
		return RETURN_VIEW_PREFIX + "equipment/searchForNode";
	}

	@RequestMapping(value = "category/presearchforequipmentupdate", method = RequestMethod.GET)
	public String preSearchForEquipmentUpdate(ModelMap modelMap)
			throws Exception {
		return RETURN_VIEW_PREFIX + "category/searchForEquipmentUpdate";
	}

	@RequestMapping(value = "equipment/ajaxautocomplete", method = RequestMethod.GET)
	@ResponseBody
	public String equipmentAutoComplete(@RequestParam("q") String keyword,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) throws Exception {
		List<Equipment> equipments = searchService.searchEquipments(keyword);
		JsonArray array = new JsonArray();
		JsonObject object = null;
		for (Equipment equipment : equipments) {
			object = new JsonObject();
			object.addProperty("name", equipment.getEquipmentName());
			object.addProperty("id", equipment.getEquipmentId());
			array.add(object);
		}
		return array.toString();
	}

	@RequestMapping(value = "category/ajaxautocomplete", method = RequestMethod.GET)
	@ResponseBody
	public String categoryAutoComplete(@RequestParam("q") String keyword,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) throws Exception {
		List<Category> categorys = searchService.searchCategorys(keyword);
		JsonArray array = new JsonArray();
		JsonObject object = null;
		List<Category> tempCategorys = null;
		for (Category category : categorys) {
			object = new JsonObject();
			tempCategorys = categoryService.getSuperCategoryChain(category);
			tempCategorys.add(category);

			object.addProperty("name", category.getCategoryName());
			object.addProperty("desc", getNavigate(tempCategorys));
			object.addProperty("id", category.getCategoryId());
			array.add(object);
		}
		return array.toString();
	}

	public static String getNavigate(List<Category> categories) {
		StringBuffer sb = new StringBuffer();
		for (Category category : categories) {
			sb.append(category.getCategoryName()).append(">");
		}
		String result = sb.toString();
		return result.substring(0, result.length() - 1);
	}

}